************************************************************************************************
// Dataanalysis: Intergenerational transmission of party affiliation within political families *
************************************************************************************************

cls
clear all
clear matrix
set more off, perm
cd "E:/ProjData/Parents politicians/"
capture log close
log using "C:/Userdata/Shared/Logs/Parent politicians/dataanalysis.text", replace text

use "dataset.dta", clear


********************************************************************************
// Regression macros
********************************************************************************

global tex "C:/Userdata/Shared/Output/Parents Politicians/tex/"
global img "C:/Userdata/Shared/Output/Parents Politicians/img/"
global parties c fp kd m mp s v sd
global stata se b(3) star(* 0.10 ** 0.05 *** 0.01)
global latex replace label se nogaps b(3) compress booktabs nonotes varlabels(_cons "Constant") nomtitles
global stats1 stats(sample partyshare r2_a N, fmt(0 0 3 0) layout("\multicolumn{1}{c}{@}" "@" "\multicolumn{1}{c}{@}") label("Sample" "Baseline prob." "Adjusted R2" "Observations"))
global statsM stats(sample r2_a N, fmt(0 3 0) layout("\multicolumn{1}{c}{@}" "@" "\multicolumn{1}{c}{@}") label("Sample" "Adjusted R2" "Observations"))
global statsL stats(sample r2_p N p0, fmt(0 3 0 3) layout("\multicolumn{1}{c}{@}" "@" "\multicolumn{1}{c}{@}" "@") label("Sample" "Pseudo R2" "Observations" "Mean p.p. with cov at 0"))
global stats2 stats(parents partyshare r2_a N, fmt(0 0 3 0) layout("\multicolumn{1}{c}{@}" "@" "\multicolumn{1}{c}{@}") label("Parents" "Baseline prob." "Adjusted R2" "Observations"))
global stats3 stats(sample parents r2_a N, fmt(0 0 3 0) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "@") label("Sample" "Parents" "Adjusted R2" "Observations"))
global stats4 stats(parents r2_a N, fmt(0 3 0) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") label("Parents" "Adjusted R2" "Observations"))
global stats5 stats(parents dv r2_a N, fmt(0 0 3 0) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") label("Parents" "Dep. Variable" "Adjusted R2" "Observations"))

global include "big8_i == 1"
global include_par "(firstyear > firstyear_mor | firstyear_mor == .) & (firstyear > firstyear_far | firstyear_far == .)"
global include_gpar "(firstyear > firstyear_mormor | firstyear_mormor == .) & (firstyear > firstyear_farfar | firstyear_farfar == .) & (firstyear > firstyear_farmor | firstyear_farmor == .) & (firstyear > firstyear_morfar | firstyear_morfar == .)"
global include_sibs "firstyear > firstyear_sibs"


********************************************************************************
// Numbers referred to in the text
********************************************************************************

* Share of parents
su any_parent if (riksdag == 1 | ko14 == 1 | lt14 == 1) & lopnrmor != . & lopnrfar != . & floor(FodArMan/100) > 1970




********************************************************************************
// Table 1: Benchmark results
********************************************************************************

file open table using "$tex/table_main.tex", write replace
file write table "\begin{tabular}{l*{3}{D{.}{.}{4.3}}}" _n
file write table "\toprule" _n
file write table "&\multicolumn{1}{c}{Proportion}&\multicolumn{1}{c}{SE}&\multicolumn{1}{c}{Observations}\\" _n
file write table "\midrule" _n

* Heading 1
file write table "&&&\\" _n
file write table "\multicolumn{4}{l}{\emph{Main sample (every candidate with a parent politician)}}\\" _n

* Parents (main sample)
ci proportions sameparty if big8_pars > 0 & $include & $include_par
	local p : di %4.3fc r(proportion)
//	local se : di %4.3fc r(se)													// Clustered standard errors are used. Estimated below. 
	local n : di %6.0fc r(N)
reg sameparty if big8_pars > 0 & $include & $include_par, cluster(muni)
	local se : di %4.3fc _se[_cons]
file write table "Same party as parent&`p'&`se'&\multicolumn{1}{c}{`n'}\\" _n

* Heading 2
file write table "&&&\\" _n
file write table "\multicolumn{4}{l}{\emph{Every candidate with either a parent, sibling or grand parent politician}}\\" _n

* Parents (when no gpars or sibs)
ci proportions sameparty if big8_pars > 0 & any_sibs == 0 & any_gpars == 0 & $include & $include_par
	local p : di %4.3fc r(proportion)
//	local se : di %4.3fc r(se)
	local n : di %6.0fc r(N)
reg sameparty if big8_pars > 0 & $include & $include_par, cluster(muni)
	local se : di %4.3fc _se[_cons]
file write table "Same party as parent&`p'&`se'&\multicolumn{1}{c}{`n'}\\" _n

* Siblings
ci proportions sameparty if any_pars == 0 & big8_sibs > 0 & any_gpars == 0 & $include & $include & $include_sibs
	local p : di %4.3fc r(proportion)
//	local se : di %4.3fc r(se)
	local n : di %6.0fc r(N)
reg sameparty if big8_pars > 0 & $include & $include_par, cluster(muni)
	local se : di %4.3fc _se[_cons]
file write table "Same party as sibling&`p'&`se'&\multicolumn{1}{c}{`n'}\\" _n

* Grand parents
ci proportions sameparty if any_pars == 0 & any_sibs == 0 & big8_gpars > 0 & $include & $include_gpar
	local p : di %4.3fc r(proportion)
//	local se : di %4.3fc r(se)
	local n : di %6.0fc r(N)
reg sameparty if big8_pars > 0 & $include & $include_par, cluster(muni)
	local se : di %4.3fc _se[_cons]
file write table "Same party as grand parent&`p'&`se'&\multicolumn{1}{c}{`n'}\\" _n

* Heading 3
file write table "&&&\\" _n
file write table "\multicolumn{4}{l}{\emph{Expected proportion candidates from same party randomly drawn from:}}\\" _n

* Calculate Herfindahl / Simpson / Effective parties
file write table "Eight parties of equal size&0.125&&\\" _n
preserve
use "D:/SCB_ConPol/Stata harmonized/NomVald/old/Nominerade.dta", clear
keep if inlist(Partikod,1,2,3,4,5,55,68,110)
sort valar
collapse (firstnm) Partikod, by(LopNr)
entropyetc Partikod, gen(3=simpson)
su simpson
local p : di %4.3fc r(mean)
restore
file write table "All candidates 1982--2014&`p'&&\\" _n

* And adjusted for space and time
su partyshare if big8_pars > 0 & big8_i == 1
local p : di %4.3fc r(mean)
file write table "Same municipality and year&`p'&&\\" _n

* Finish table
file write table "&&&\\" _n
file write table "\bottomrule" _n
file write table "\end{tabular}" _n
file close table



********************************************************************************
// Regression tables and figures
********************************************************************************

* Sample restriction shared by all the following regression tables 
preserve
keep if $include
keep if big8_pars > 0
keep if $include_par 

** Robustness benchmark results **
eststo: reg sameparty, cluster(muni)
	estadd local sample "All"
	estadd local partyshare "No"
eststo: reg sameparty partyshare, cluster(muni)
	estadd local sample "All"
	estadd local partyshare "Yes"
eststo: reg sameparty partyshare if tophalf == 1, cluster(muni)
	estadd local sample "Top half"
	estadd local partyshare "Yes"
eststo: reg sameparty partyshare if firstage > 30, cluster(muni)
	estadd local sample "Over 30"
	estadd local partyshare "Yes"
eststo: reg sameparty partyshare if muni_mor != muni & muni_far != muni, cluster(muni)
	estadd local sample "Other muni"
	estadd local partyshare "Yes"
esttab, $stata
esttab using "${tex}/table_robustness.tex", $latex $stats1 drop(partyshare)
eststo clear


** Mechanisms analysis, county **
eststo: reg sameparty, cluster(muni) 
	estadd local sample "All"
eststo: reg sameparty if !missing(lanlivingpol) & !missing(incpcdif), cluster(muni) 
	estadd local sample "Rest."	
eststo: reg sameparty lanlivingpol if !missing(lanlivingpol) & !missing(incpcdif), cluster(muni)
	estadd local sample "Rest."
eststo: reg sameparty mostlylanlivingpol if !missing(lanlivingpol) & !missing(incpcdif), cluster(muni)
	estadd local sample "Rest."
eststo: reg sameparty incpcdif if !missing(lanlivingpol) & !missing(incpcdif), cluster(muni)
	estadd local sample "Rest."
esttab, $stata
esttab using "${tex}/table_mechanisms.tex", $latex $statsM 
eststo clear


** Mechanism analysis, but based on SAMS **
eststo: reg sameparty, cluster(muni) 
	estadd local sample "All"
eststo: reg sameparty if !missing(samslivingpol), cluster(muni) 
	estadd local sample "Rest."	
eststo: reg sameparty samslivingpol if !missing(samslivingpol), cluster(muni)
	estadd local sample "Rest."
eststo: reg sameparty mostlysamslivingpol if !missing(samslivingpol), cluster(muni)
	estadd local sample "Rest."
esttab, $stata
esttab using "${tex}/table_samsliving.tex", $latex $statsM 
eststo clear


* Restore sample
restore

** Intermediate steps materialistic pathway **
label var i_zink "Sdt. Income ind."

* Income correlation: merged*
eststo clear
eststo: reg i_zink f_zink if big8_pars > 0 & $include & $include_par, cluster(muni)
	estadd local parents "Two"
eststo: reg i_zink m_zink if big8_pars > 0 & $include & $include_par, cluster(muni)
	estadd local parents "Two"
eststo: reg i_zink f_zink m_zink if big8_pars > 0 & $include & $include_par, cluster(muni)
	estadd local parents "Two"
eststo: reg all_i i_zink if big8_pars > 0 & $include & $include_par, cluster(muni)
	estadd local parents "Two"
esttab, $stata
esttab using "${tex}/table_incmerged.tex", replace label se nogaps b(3) compress booktabs nonotes varlabels(_cons "Constant") mtitles("I.Std.Inc." "I.Std.Inc." "I.Std.Inc." "I.CR.Nom") $stats4 
eststo clear

label var i_zink "Sdt. Income"


** Party matrix **
preserve
keep if $include
keep if big8_pars > 0 
keep if $include_par
collapse (mean) c_i fp_i kd_i m_i mp_i s_i v_i sd_i, by(parentsparty)
drop if parentsparty == ""
foreach p in c fp kd m mp s v sd {
	gen `p' = `p'_i*100
	tostring `p', replace format(%4.1f) force

	* Make center column bold
	replace `p' = "\multicolumn{1}{B{.}{.}{-1}}{" + `p' + "}" if lower(parentsparty) == "`p'"
	drop `p'_i
}
replace parentsparty = upper(parentsparty)
order parentsparty v mp s c fp m kd sd
gen tex = parentsparty + "&" + v + "&" + mp + "&" + s + "&" + c + "&" + fp + "&" + m + "&" + kd + "&" + sd + "\\"
file open tex using "C:/Userdata/Shared/Output/Parents Politicians/tex/partymatrix.tex", write replace
file write tex "\begin{tabular}{l*{8}{D{.}{.}{-1}}}" _n
file write tex "\toprule" _n
file write tex "&\multicolumn{1}{c}{V}&\multicolumn{1}{c}{MP}&\multicolumn{1}{c}{S}&\multicolumn{1}{c}{C}&\multicolumn{1}{c}{FP}&\multicolumn{1}{c}{M}&\multicolumn{1}{c}{KD}&\multicolumn{1}{c}{SD}\\" _n
file write tex "\midrule" _n
gen count = _n
foreach p in V MP S C FP M KD SD  {
	su count if parentsparty == "`p'"
	local tex = tex[`r(mean)']
	file write tex "`tex'" _n
}
file write tex "\bottomrule" _n
file write tex "\end{tabular}" _n
file close tex
restore

** Intergenerational transmission over time **
clear matrix
levelsof firstyear
foreach k in `r(levels)' {
	preserve
	capture reg sameparty if firstyear == `k' & big8_pars > 0 & $include & $include_par, cluster(muni)
	if _rc == 0 {
		matrix define a=e(b)
		scalar define a1=a[1,1]
		matrix define v=e(V)
		scalar define v1=sqrt(v[1,1])
		scalar define upper=a1+1.96*v1
		scalar define lower=a1-1.96*v1
		scalar define aa=`k'
		matrix define constestimate=[nullmat(constestimate)\aa,a1,upper,lower]
	}
	restore
}
svmat constestimate
preserve
keep constestimate1 constestimate2 constestimate3 constestimate4 
#delimit ;
gr tw 	(rcap constestimate4 constestimate3 constestimate1) 
		(scatter constestimate2 constestimate1), 
		graphregion(color(white)) 
		legend(label(1 "95% confidence band") label(2 "Point estimate") region(lcolor(white))) 
		ytitle("Estimated constant") 
		xtitle("Election year") legend(off)
		yla(,nogrid) scheme(lean1);
#delimit cr;
graph export "${img}/effectovertime.pdf", as(pdf) replace 
graph export "${img}/effectovertime.eps", as(eps) replace 
restore
drop constestimate1 constestimate2 constestimate3 constestimate4 


** Gender analysis **
eststo: reg sameparty c.female##c.any_mor if big8_pars == 1 & $include & $include_par, cluster(muni) // 1 par because # of pols otherwise correlated with any_mor
	estadd local parents "One"
	estadd local partyshare "No"
	
eststo: reg sameparty partyshare c.female##c.any_mor if big8_pars == 1 & $include & $include_par, cluster(muni)
	estadd local parents "One"
	estadd local partyshare "Yes"
	
esttab, $stata
esttab using "${tex}/table_gender.tex", $latex $stats2 drop(partyshare)
eststo clear


** Bloc analysis for when parents run for different parties **
eststo: reg all_i all_mor all_far if big8_pars == 2 & big8_i == 1 & sd_i == 0 & sd_pars == 0 & $include & $include_par, cluster(muni)
	estadd local sample "All"
	estadd local parents "Two"
	estadd local partyshare "No"
eststo: reg all_i all_mor all_far if big8_pars == 2 & big8_i == 1 & sd_i == 0 & sd_pars == 0 & female == 0 & $include & $include_par, cluster(muni)
	estadd local sample "Men"
	estadd local parents "Two"
	estadd local partyshare "No"
eststo: reg all_i all_mor all_far if big8_pars == 2 & big8_i == 1 & sd_i == 0 & sd_pars == 0 & female == 1 & $include & $include_par, cluster(muni)
	estadd local sample "Women"
	estadd local parents "Two"
	estadd local partyshare "No"
esttab, $stata
esttab using "${tex}/table_bloc.tex", $latex $stats3
eststo clear


** Logit version of mechanisms, county **
eststo: logit sameparty, cluster(muni) 
	estadd local sample "All"
	predict p0 if e(sample)
	su p0
	estadd scalar p0 = r(mean)
	drop p0
eststo: logit sameparty if !missing(lanlivingpol) & !missing(incpcdif), cluster(muni) 
	estadd local sample "Rest."
	predict p0 if e(sample)
	su p0
	estadd scalar p0 = r(mean)
	drop p0
eststo: logit sameparty lanlivingpol if !missing(lanlivingpol) & !missing(incpcdif), cluster(muni)
	estadd local sample "Rest."
	gen bk = lanlivingpol
	replace lanlivingpol = 0
	predict p0 if e(sample)
	su p0
	estadd scalar p0 = r(mean)
	replace lanlivingpol = bk
	drop p0 bk
eststo: logit sameparty mostlylanlivingpol if !missing(lanlivingpol) & !missing(incpcdif), cluster(muni)
	estadd local sample "Rest."
	gen bk = mostlylanlivingpol
	replace mostlylanlivingpol = 0
	predict p0 if e(sample)
	su p0
	estadd scalar p0 = r(mean)
	replace mostlylanlivingpol = bk
	drop p0 bk
eststo: logit sameparty incpcdif if !missing(lanlivingpol) & !missing(incpcdif), cluster(muni)
	estadd local sample "Rest."
	gen bk = incpcdif
	replace incpcdif = 0
	predict p0 if e(sample)
	su p0
	estadd scalar p0 = r(mean)
	replace incpcdif = bk
	drop p0 bk
esttab, $stata
esttab using "${tex}/table_mechanisms_logit.tex", $latex $statsL eqlabels(none)
eststo clear


** Results for municipal, county and parliament separately **
foreach k in KO LT RI {
	
  *  Benchmark table

	file open table using "$tex/table_main_`k'.tex", write replace
	file write table "\begin{tabular}{l*{3}{D{.}{.}{4.3}}}" _n
	file write table "\toprule" _n
	file write table "&\multicolumn{1}{c}{Proportion}&\multicolumn{1}{c}{SE}&\multicolumn{1}{c}{Observations}\\" _n
	file write table "\midrule" _n

	* Heading 1
	file write table "&&&\\" _n
	file write table "\multicolumn{4}{l}{\emph{Main sample (every candidate with a parent politician)}}\\" _n

	* Parents (main sample)
	ci proportions sameparty if big8_pars > 0 & $include & $include_par & modevaltyp_i == "`k'"
		local p : di %4.3fc r(proportion)
	//	local se : di %4.3fc r(se)
		local n : di %6.0fc r(N)
	reg 		   sameparty if big8_pars > 0 & $include & $include_par & modevaltyp_i == "`k'", cluster(muni)
		local se : di %4.3fc _se[_cons]	
	file write table "Same party as parent&`p'&`se'&\multicolumn{1}{c}{`n'}\\" _n

	* Heading 2
	file write table "&&&\\" _n
	file write table "\multicolumn{4}{l}{\emph{Every candidate with either a parent, sibling or grand parent politician}}\\" _n

	* Parents (when no gpars or sibs)
	ci proportions sameparty if big8_pars > 0 & any_sibs == 0 & any_gpars == 0 & $include & $include_par & modevaltyp_i == "`k'"
		local p : di %4.3fc r(proportion)
	//	local se : di %4.3fc r(se)
		local n : di %6.0fc r(N)
	reg 		   sameparty if big8_pars > 0 & any_sibs == 0 & any_gpars == 0 & $include & $include_par & modevaltyp_i == "`k'", cluster(muni)
		local se : di %4.3fc _se[_cons]	
	file write table "Same party as parent&`p'&`se'&\multicolumn{1}{c}{`n'}\\" _n

	* Siblings
	ci proportions sameparty if any_pars == 0 & big8_sibs > 0 & any_gpars == 0 & $include & $include_sibs & modevaltyp_i == "`k'"
		local p : di %4.3fc r(proportion)
	//	local se : di %4.3fc r(se)
		local n : di %6.0fc r(N)
	reg 		   sameparty if any_pars == 0 & big8_sibs > 0 & any_gpars == 0 & $include & $include_sibs & modevaltyp_i == "`k'", cluster(muni)
		local se : di %4.3fc _se[_cons]	
	file write table "Same party as sibling&`p'&`se'&\multicolumn{1}{c}{`n'}\\" _n

	* Grand parents
	ci proportions sameparty if any_pars == 0 & any_sibs == 0 & big8_gpars > 0 & $include & $include_gpar & modevaltyp_i == "`k'"
		local p : di %4.3fc r(proportion)
	//	local se : di %4.3fc r(se)
		local n : di %6.0fc r(N)
	reg 		   sameparty if any_pars == 0 & any_sibs == 0 & big8_gpars > 0 & $include & $include_gpar & modevaltyp_i == "`k'", cluster(muni)
		local se : di %4.3fc _se[_cons]	
	file write table "Same party as grand parent&`p'&`se'&\multicolumn{1}{c}{`n'}\\" _n


	* Finish table
	file write table "&&&\\" _n
	file write table "\bottomrule" _n
	file write table "\end{tabular}" _n
	file close table
	
}



** Transmission for different timing on when the parent was a politician **

gen firstyear_parent = .
replace firstyear_parent = firstyear_mor if firstyear_far == .  
replace firstyear_parent = firstyear_far if firstyear_mor == .  
gen fodar = real(substr(string(FodArMan),1,4))									// Note real()- and string()
gen age_par_nomi = firstyear_parent - fodar

label var age_par_nomi "Age when parent was nominated" 


* Twoway histogram with transmission
tw (hist age_par_nomi, ysc(off axis(2)) yaxis(2) discrete) (lpoly sameparty age_par_nomi if age_par_nomi < 61, yaxis(1) ysc(alt) yla(0(0.2)0.8) xtitle("Age when parent was nominated") ytitle("Share with same party as parent (smoothed)" )), scheme(lean1) legend(off)
graph export "${img}/timing_pars_nomi.pdf", as(pdf) replace 
graph export "${img}/timing_pars_nomi.eps", as(eps) replace 

replace age_par_nomi = . if age_par_nomi < 0  
global overtime "(firstyear_mor != . & firstyear_far ==.) | (firstyear_mor == . & firstyear_far != .) & age_par_nomi > 0 "



************************
capture log close
clear all
